package edu.sctu.treebuilder.day02;


/**
 * Created by apple on 05/02/2018.
 */
public enum State {

    DATA {
        @Override
        void read(StateController controller, CharacterReader reader) {

            char ch = reader.current();

            switch (ch) {

                case '<':
                    controller.advanceTransition(TagOpen);
                    break;

                case CharacterReader.EOF:

                    controller.emit(new Token.EOF());
                    break;

                default:
                    String data = reader.consumeData();
                    controller.emit(data);

                    break;

            }
        }
    },
    TagOpen {
        @Override
        void read(StateController controller, CharacterReader reader) {

            char ch = reader.current();

            switch (ch) {

                case '/':
                    controller.advanceTransition(EndTagOpen);
                    break;

                default:

                    if (reader.matchesLetter()) {

                        controller.createTagPending(true);
                        controller.transition(TagName);
                    }
                    break;

            }

        }
    },
    TagName {
        @Override
        void read(StateController controller, CharacterReader reader) {


            String tagName = reader.consumeTagName();
            controller.tagPending.appendTagName(tagName);

            char ch = reader.consume();

            switch (ch) {

                case '>':

                    controller.emitTagPending();
                    controller.transition(DATA);
                    break;


                default:

                    break;
            }
        }
    },
    EndTagOpen {
        @Override
        void read(StateController controller, CharacterReader reader) {


            if (reader.matchesLetter()) {

                controller.createTagPending(false);
                controller.transition(TagName);
            }

        }
    };


    abstract void read(StateController controller, CharacterReader reader);
}
